<?php

namespace common\service;


use common\model\MessageModel;
use common\model\TenantUserDepartmentModel;
use common\model\TenantUserModel;
use common\model\TenantUserRoleModel;
use support\ErrorCode;
use Webman\Stomp\Client;

class FixService
{

    /**
     * @return void
     */
    public function formatMessageCard0628($filter, $page)
    {
        $page = $page ?? [1, 500];
        $messageList = model(MessageModel::class)->where(['content_type' => 'card_message'])->where($filter)->page($page[0], $page[1])->select();
        if (empty($messageList)) {
            throw_strack_exception('no more message', ErrorCode::MATCH_RECORD_NOT_FOUND);
        }
        foreach (array_chunk($messageList, 100) as $messageListI) {
            Client::send('card_message_format0628', build_queue_data_with_xu(['ids' => implode(',', array_column($messageListI, 'id'))]), 2);
        }

    }

    /**
     * 转换租户用户到tenantUserDepartment/ tenantUserRole
     * @return array
     * @throws \Exception
     */
    public function transferTenantUserData0427()
    {
        $tenantUserList = model(TenantUserModel::class)->select();

        $tuFiltered = [];
        $tuNeedDelete = [];
        $tudd = [];
        $turd = [];
        foreach ($tenantUserList as &$tenantUser) {

            $tenantUser['turk'] = sprintf('%d-%d-%d', $tenantUser['tenant_id'], $tenantUser['user_id'], $tenantUser['role_id']);
            if (empty($tuFiltered[$tenantUser['tenant_id']][$tenantUser['user_id']])) {
                $tuFiltered[$tenantUser['tenant_id']][$tenantUser['user_id']] = $tenantUser['id'];
            } else {
                $tuNeedDelete[] = $tenantUser['id'];
            }
            if ($tenantUser['department_id'] > 0) {
                $tenantUser['tudk'] = sprintf('%d-%d-%d', $tenantUser['tenant_id'], $tenantUser['user_id'], $tenantUser['department_id']);
                if (empty($tudd[$tenantUser['tudk']])) {
                    $tudd[$tenantUser['tudk']] = [
                        'tenant_user_id' => $tuFiltered[$tenantUser['tenant_id']][$tenantUser['user_id']],
                        'department_id' => $tenantUser['department_id'],
                        'created' => time(),
                        'created_by' => fill_created_by(),
                    ];
                }
            }

            if ($tenantUser['role_id'] > 0) {
                $tenantUser['turk'] = sprintf('%d-%d-%d', $tenantUser['tenant_id'], $tenantUser['user_id'], $tenantUser['role_id']);
                if (empty($turd[$tenantUser['turk']])) {
                    $turd[$tenantUser['turk']] = [
                        'tenant_user_id' => $tuFiltered[$tenantUser['tenant_id']][$tenantUser['user_id']],
                        'role_id' => $tenantUser['role_id'],
                        'created' => time(),
                        'created_by' => fill_created_by(),
                    ];
                }
            }
        }

        if (!empty($tuNeedDelete)) {
            model(TenantUserModel::class)->where(['id' => ['IN', $tuNeedDelete]])->delete();
        }
        if (!empty($tudd)) {
            $tudd = array_values($tudd);
            model(TenantUserDepartmentModel::class)->execute("truncate table tenant_user_department");
            model(TenantUserDepartmentModel::class)->addAll($tudd);
        }
        if (!empty($turd)) {
            $turd = array_values($turd);
            model(TenantUserRoleModel::class)->execute("truncate table tenant_user_role");
            model(TenantUserRoleModel::class)->addAll($turd);
        }

        return ['deleted' => $tuNeedDelete, 'tenant_user_department_added' => $tudd, 'tenant_user_role_added' => $turd];
    }

}
